/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SingleStepPanel.java
 *
 * Created on 8-dic-2008, 14.46.24
 */

package it.unifi.dsi.blide.run.imp;

import it.unifi.dsi.blitese.localenv.LocalEnvironment;
import it.unifi.dsi.blitese.localenv.SingleStepper;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author panks
 */
public class SingleStepPanel extends javax.swing.JPanel implements ChangeListener {

    private LocalEnvironment environment;
    private SingleStepper stepper;

    /** Creates new form SingleStepPanel */
    public SingleStepPanel() {
        initComponents();

        environment = BliteLocalEnvTopComponent.findInstance().getEnvironment();
        stepper = environment.getStepper();
        stepper.addChangeListener(this);

        jCheckBox.setSelected(stepper.isEnabled());
        jButton.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBox = new javax.swing.JCheckBox();
        jButton = new javax.swing.JButton();

        jCheckBox.setText(org.openide.util.NbBundle.getMessage(SingleStepPanel.class, "SingleStepPanel.jCheckBox.text_1")); // NOI18N
        jCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxActionPerformed(evt);
            }
        });

        jButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/unifi/dsi/blide/run/imp/run24.png"))); // NOI18N
        jButton.setText(org.openide.util.NbBundle.getMessage(SingleStepPanel.class, "SingleStepPanel.jButton.text_1")); // NOI18N
        jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
            .addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxActionPerformed
        stepper.setEnabled(jCheckBox.isSelected());
}//GEN-LAST:event_jCheckBoxActionPerformed

    private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActionPerformed

        jButton.setEnabled(false);

        stepper.letgo();
    }//GEN-LAST:event_jButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton;
    private javax.swing.JCheckBox jCheckBox;
    // End of variables declaration//GEN-END:variables

    public void stateChanged(ChangeEvent e) {
        jCheckBox.setSelected(stepper.isEnabled());

        if (stepper.isEnabled()) {
            jButton.setEnabled(stepper.isSteped());
        }
    }

}
